﻿using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
/// <summary>
/// Some methods for this class of extended string, responsible for the conversion of some categories
/// </summary>
public static class StringExtend
{
  
    public static object StringToEnum(this string v, Type enumType)
    {
        if (!enumType.IsEnum) { 
            Debuger.LogError ("type " + enumType + " not is Enum");
        }
        Array Arrays = Enum.GetValues(enumType);
        for (int i = 0; i < Arrays.Length; i++)
        {
            if (Arrays.GetValue(i).ToString().Equals(v))
            {
                return Arrays.GetValue(i);
            }
        }
        return Arrays.GetValue(0);
    }
    public static T StringToEnum<T>(this string v)
    {
        if (!typeof(T).IsEnum) { 
            Debuger.LogError ("type " +typeof(T) + " not is Enum");
        }
        Array Arrays = Enum.GetValues(typeof(T));
        for (int i = 0; i < Arrays.Length; i++)
        {
            if (Arrays.GetValue(i).ToString().Equals(v))
            {
                return (T)Arrays.GetValue(i);
            }
        }
        return (T)Arrays.GetValue(0);
    }

    public static bool StringToBool(this string v)
    {
        if (v.Equals("True") || v.Equals("1") || v.Equals("true") || v.Equals("yes") || v.Equals("Yes"))
            return true;
        return false;
    }
    public static Vector3 StringToVector3(this string v)
    {
      //  v = v.Substring(1, v.Length - 2);
        string[] s = v.Split(',');
        return new Vector3(float.Parse(s[0]), float.Parse(s[1]), float.Parse(s[2]));
    }
    public static string Vector3ToString(this Vector3 v)
    {
        return v.x + "," + v.y + "," + v.z;
    }

    public static Vector2 StringToVector2(this string v)
    {
        string[] s = v.Split(',');
        return new Vector2(float.Parse(s[0]), float.Parse(s[1]));
    }
    public static string Vector2ToString(this Vector2 v)
    {
        return v.x + "," + v.y;
    }
    public static Quaternion StringToQuaternion(this string v)
    {
        string[] s = v.Split(',');
        return new Quaternion(float.Parse(s[0]), float.Parse(s[1]), float.Parse(s[2]), float.Parse(s[3]));
    }
    public static string QuaternionToString(this Quaternion v)
    {
        return v.x + "," + v.y + "," + v.z+ "," + v.w;
    }

    public static int BytesToInt(this byte[] src)
    {
        int value;
        value = (int)((src[0] & 0xFF)
            | ((src[1] & 0xFF) << 8)
            | ((src[2] & 0xFF) << 16)
            | ((src[3] & 0xFF) << 24));
        return value;
    }
    public static byte[] IntToBytes(this int value)
    {
        byte[] src = new byte[4];
        src[3] = (byte)((value >> 24) & 0xFF);
        src[2] = (byte)((value >> 16) & 0xFF);
        src[1] = (byte)((value >> 8) & 0xFF);
        src[0] = (byte)(value & 0xFF);
        return src;
    }
    public static int StringToInt(this string v)
    {
        int var_result;
        if (int.TryParse(v,out var_result))
        {
            return var_result;
        }
        return int.MinValue;
    }

}
